﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Pages_MasterPageFontEnd : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Id"] == null)
        {
            lblUser.Visible = false;
            btnLogin.Visible = true;
            btnAccManage.Visible = false;
            btnLogout.Visible = false;
        }
        else
        {
            lblUser.Visible = true;
            lblUser.Text ="Welcome "+ Session["UserName"].ToString();
            txtUsername.Visible = false;
            txtPassword.Visible = false;
            btnAccManage.Visible = true;
            btnLogin.Visible = false;
            btnLogout.Visible = true;
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        StudentBUS st = new StudentBUS();
        StaffBUS staff = new StaffBUS();
        string user = txtUsername.Text;
        string pass = txtPassword.Text;
        DataSet stDs = st.studentLogin(user, pass);
        DataSet staffDs = staff.staffLogin(user, pass);
        if (staffDs.Tables[0].Rows.Count > 0)
        {
            Session["Id"] = staffDs.Tables[0].Rows[0]["staffId"];
            Session["UserName"] = staffDs.Tables[0].Rows[0]["userName"];
            Session["Role"] = "Staff";
            Response.Redirect(Request.RawUrl);
        }
        else if (stDs.Tables[0].Rows.Count > 0)
        {
            Session["Id"] = stDs.Tables[0].Rows[0]["studentId"];
            Session["UserName"] = stDs.Tables[0].Rows[0]["userName"];
            Session["Role"] = "Student";
            Response.Redirect(Request.RawUrl);
        }
        else
        {
            lblUser.Text = "UserName Or Password Is Wrong";
            lblUser.Visible = true;
        }
    }
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Session["Id"] = null;
        Session["UserName"] = null;
        Response.Redirect(Request.RawUrl);
    }
    protected void btnAccManage_Click(object sender, EventArgs e)
    {
        if ("Student".Equals(Session["Role"]))
        {
            Response.Redirect("~/Pages/StudentPages/StudentsHome.aspx");
        }
        else if ("Staff".Equals(Session["Role"]))
        {
            Response.Redirect("~/Pages/StaffPages/StaffHome.aspx");
        }
        else if ("Manager".Equals(Session["Role"]))
        {
            Response.Redirect("~/Pages/ManagerPages/ManagerHome.aspx");
        }
    }
}
